package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer userId) {
        return roleDao.findRoleIdsByUserId(userId);
    }

    @Override
    public void add(Role role,Integer[] menuIds,Integer[] permissionIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        for (Integer permissionId : permissionIds) {
            Map map1 = new HashMap();
            map1.put("roleId",roleId);
            map1.put("permissionId",permissionId);
            roleDao.addRoleIdAndPermissionId(map1);
        }
        for (Integer menuId : menuIds) {
            Map map1 = new HashMap();
            map1.put("roleId",roleId);
            map1.put("menuId",menuId);
            roleDao.addRoleIdAndMenuId(map1);
        }
   }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> roles = roleDao.selectByCondition(queryString);
        long total = roles.getTotal();
        List<Role> rows = roles.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {

        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        Integer roleId = role.getId();
        System.out.println(roleId);
        roleDao.edit(role);
        roleDao.deleteMenuIdsByRoleId(roleId);
        for (Integer menuId : menuIds) {
            Map map1 = new HashMap();
            map1.put("roleId",roleId);
            map1.put("menuId",menuId);
            roleDao.addRoleIdAndMenuId(map1);
        }
        roleDao.deletePermissionIdsByRoleId(roleId);
        for (Integer permissionId : permissionIds) {
            Map map1 = new HashMap();
            map1.put("roleId",roleId);
            map1.put("permissionId",permissionId);
            roleDao.addRoleIdAndPermissionId(map1);
        }
    }

    @Override
    public void deleteRelo(Integer id) {
        roleDao.deleteMenuIdsByRoleId(id);
        roleDao.deletePermissionIdsByRoleId(id);
        roleDao.deleteReloById(id);
    }
}
